// import { login } from '@/api/user'
import Cookie from "js-cookie"
import Vue from "vue"
// console.log('state+++++++++++++++++++++++++++++++++++++++++++++')
const state = () =>  ({
  token: Cookie.get('token') || '',
  name: Cookie.get('name') || '',
  loginDialog: false,
  info: Cookie.get('info') || {},
  showComment: '',
  commentCount: 0,
  headImg: Cookie.get('headImg') || null,
  commentList: [],
  personalId: Cookie.get('personalId') || ''
})

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
    Cookie.set('token', token)
    // localStorage.setItem('token', token)
  },
  SET_PERSONAL_ID: (state, personalId) => {
    state.token = personalId
    Cookie.set('personalId', personalId)
  },
  SET_TOKEN: (state, token) => {
    state.token = token
    Cookie.set('token', token)
    // localStorage.setItem('token', token)
  },
  SET_NAME: (state, name) => {
    state.name = name
    Cookie.set('name', name)
  },
  SET_LOGIN: (state, data) => {
    state.loginDialog = data
  },
  SET_INFO: (state, info) => {
    state.info = info
    // if (info.length > 0) {
      Cookie.set('info', info)
    // }
  },
  SET_HEADIMG: (state, img) => {
    state.headImg = img
    Cookie.set('headImg', img)
  },
  // 评论下级组件文本框是否展示
  SET_SHOWCOMMENT:(state, show) => {
    state.showComment = show
  },
  SET_COMMENT_COUNT:(state, count) => {
    state.commentCount = count
  },
  SET_COMMENTLIST:(state, nodes) => {
    const map = new Map();
    const tree = [];
    // 将每个节点存入map中
    for (const node of nodes) {
      map.set(node.data_id, { ...node, children: [] });
    }
    // 将每个节点添加到其父节点下
    for (const node of map.values()) {
      if (node.parent_id === 0) {
        tree.push(node);
      } else {
        const parent = map.get(node.parent_id);
        console.log(parent);
        if (parent) {
          parent.children.push(node);
        }
      }
    }
    state.commentList = tree
  }
}

const getters = {
  getShowComment: state => state.showComment,
  getCommentList: state => state.commentList,
  getToken: state => state.token
}

const actions = {
  async nuxtServerInit({ commit }) {
    commit("SET_INFO", Cookie.get("info"));
  },
  // user logout
  logout({ commit, state, store }) {
    this.$cookies.removeAll()
    commit('SET_TOKEN', '')
    commit('SET_NAME', '')
    commit('SET_INFO', {})
    commit('SET_HEADIMG', '')
    Cookie.remove('token')
    console.log('state', state)
  },
  replyComment({commit, ctx}, params) {
    return new Promise((resolve, reject) => {
      Vue.prototype.$api.replyComment(params, {}).then(res => {
        if (res.status === '$SUCCESS') {
          commit('SET_COMMENTLIST', Array.isArray(res.result.item_list) ? res.result.item_list : [])
          resolve()
        }
      }).catch(error => {
        reject(error)
      })
    })
    // context.commit('SET_COMMENTLIST', step)
  },
  getComment({commit, ctx}, params) {
    Vue.prototype.$api.getCommentList(params, {}).then(res => {
      if (res.status === '$SUCCESS') {
        commit('SET_COMMENTLIST', Array.isArray(res.result.item_list) ? res.result.item_list : [])
        commit('SET_COMMENT_COUNT', Array.isArray(res.result.item_list) ? res.result.item_list.length : 0)
      }
    })
    // context.commit('SET_COMMENTLIST', step)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
